<?php

/*
 * Copyright (c) Madbit 2012. All Rights Reserved.
 * Reproduction in whole or in part is prohibited without the
 * written consent of the copyright owner.
 */

/**
 * Description of LoginController
 *
 * @author Alessio Fiore
 */
class LoginController {

    public function login($username, $password, $challenge,$setcookie) {
        include_once '../utils/includer.php';
        includeFileOnce("protected/database/MySqlDAO.php");
        
        $mySqlDAO = new MySqlDAO();
        
        $user = $mySqlDAO->authenticateUser($username);
        
        if (md5($challenge.$user->password) == $password) {
            // TODO DBquery;
            session_start();
            $_SESSION['uid'] = $user->idu;
            $_SESSION['username'] = $username;
            if($setcookie == "yes")
                setcookie("wd_username", $username, time()+3600,'/', "localhost", 0);
            
            return true;
        }
        else
            return false;
    }

    public function logout() {
        setcookie("wd_username", $username, time()-3600,'/', "localhost", 0);
        session_start();
        session_destroy();
    }

}

?>
